package mil.af.amc.adpetracker.web.actions.struts1.EquipmentItem;

import mil.af.amc.adpetracker.service.EquipmentItemService;
import mil.af.amc.adpetracker.service.ServiceException;
import mil.af.amc.adpetracker.web.actions.struts1.AbstractAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Please Fill in this Javadoc
 *
 * @author TSgt Jason Ferguson
 */

public class UnassignItemAction extends AbstractAction {

    private EquipmentItemService equipmentItemService;

    public void setEquipmentItemService(EquipmentItemService equipmentItemService) {
        this.equipmentItemService = equipmentItemService;
    }

    @Override
    public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {

        int itemId = Integer.parseInt(httpServletRequest.getParameter("itemId"));

        try {
            equipmentItemService.unassignItem(itemId);
        } catch (ServiceException e) {
            ActionMessage message = new ActionMessage(e.getKey());
            saveError(httpServletRequest, message);
        }

        if (httpServletRequest.getAttribute("viewItem") != null) {
            return actionMapping.findForward("itemProfile");
        }

        return actionMapping.getInputForward();
    }
}
